/* Copyright (c) 2004 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of 
 * Nordic Semiconductor. The use, copying, transfer or disclosure 
 * of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 */ 

/** @file
 * Header file for PS2 interface
 * @author Runar Kjellhaug
 *
 */

#ifndef PS2_IO_H__
#define PS2_IO_H__

#include <stdint.h>
#include <stdbool.h>
#include <Nordic\reg24lu1.h>

#define PS2_FIFO_LEVELS     5
#define PS2_PACKET_SIZE     3 // Standard mouse

#define PS2_BUFSIZE         (PS2_PACKET_SIZE * PS2_FIFO_LEVELS)

#define PS2_IRQ_ENABLE() do { IE0 = 0;  \
                              EX0 = 1;} while(0)
#define PS2_IRQ_DISABLE() do { EX0 = 0;} while(0)

//#define PS2_IRQ_SET_PRIORITY() do { PX1=1;} while(0);

sbit CIN = P0^3;
sbit DIN = P0^0;
sbit COUT = P0^2;
sbit DOUT = P0^1;

#define COUT_LOW() do { COUT = 1; } while(0)
#define COUT_HIGH() do { COUT = 0; } while(0)
#define DOUT_LOW() do { DOUT = 1; } while(0)
#define DOUT_HIGH() do { DOUT = 0; } while(0)

// PS2 COMMANDS             CMD,RESP
#define PS2_RESET           0xFF,3
#define PS2_RESEND          0xFE,3
#define PS2_SET_DEFAULT     0xF6,1
#define PS2_DISABLE_REPORT  0xF5,1
#define PS2_ENABLE_REPORT   0xF4,1
#define PS2_SET_SAMPLE_RATE 0xF3,1
#define PS2_GET_DEVICE_ID   0xF2,2    // PS2 mouse responds: "PS2_MOUSE"
#define PS2_SET_REMOTE_MODE 0xF0,1
#define PS2_SET_WRAP_MODE   0xEE,1
#define PS2_RESET_WRAP_MODE 0xEC,1
#define PS2_READ_DATA       0xEB,3    // used in "REMOTE MODE"
#define PS2_READ_DATA_NO_RET 0xEB,1
#define PS2_SET_STREAM_MODE 0xEA,1
#define PS2_STATUS_REQUEST  0xE9,4
#define PS2_SET_SCALING_1_1 0xE6,1
#define PS2_SET_SCALING_2_1 0xE7,1

#define PS2_SET_RESOLUTION  0xE8,1

// PS2 COMMAND RESPONS
#define PS2_MOUSE_STANDARD  0x00
#define PS2_MOUSE_INTELLI   0x03
#define PS2_ACK             0xFA
#define PS2_RST_RESP        0xAA
#define PS2_KEYBOARD   0xAB

// PS2 on-air frame size
#define PS2_FRAME_SIZE      0x0a
// PS2 frame pos definitions
#define PS2_BUTTONS            0
#define PS2_X_MOVEMENT         1
#define PS2_Y_MOVEMENT         2
#define PS2_Z_MOVEMENT         3

// PS2 keyboard constants

// PS2 mouse constants
#define PS2_SAMPLES_10       10
#define PS2_SAMPLES_20       20
#define PS2_SAMPLES_40       40
#define PS2_SAMPLES_60       60
#define PS2_SAMPLES_80       80
#define PS2_SAMPLES_100     100
#define PS2_SAMPLES_200     200

#define PS2_1_CNT_MM        0x00,1
#define PS2_2_CNT_MM        0x01,1
#define PS2_4_CNT_MM        0x02,1
#define PS2_8_CNT_MM        0x03,1

//uint8_t ps2_init();
uint8_t ps2_read(void);
uint8_t ps2_write(uint8_t byte, uint8_t resp_count);  //xx
void ps2_flush_fifo();
uint8_t ps2_get_fifo();
void ps2_put_fifo();
bool ps2_fifo_get_status();

#endif












